 package com.oanda.blackberry.view;
 
 import com.oanda.blackberry.restapi.AccountEntry;
 import com.oanda.blackberry.restapi.AccountStatus;
 import com.oanda.blackberry.util.DefaultController;
 import com.oanda.blackberry.util.Model;
 import com.oanda.blackberry.util.StringUtils;
 import com.oanda.blackberry.util.log.Logger;
 import java.util.Hashtable;
 import net.rim.device.api.system.Bitmap;
 import net.rim.device.api.ui.Field;
 import net.rim.device.api.ui.Font;
 import net.rim.device.api.ui.Graphics;
 import net.rim.device.api.ui.component.ListField;
 
 public class AccountListField
 {
   private AccountEntry[] listData;
   private ListField listField;
   private ListCallBack listFieldCallBack = null;
 
   Hashtable accountStatus = new Hashtable();
 
   public AccountListField(AccountEntry[] accountList) {
     this.listFieldCallBack = new ListCallBack();
 
     this.listField = new ListField() {
       protected void drawFocus(Graphics graphics, boolean on) {
       }
 
       protected int moveFocus(int amount, int status, int time) {
         int ret = super.moveFocus(amount, status, time);
         invalidate();
         return ret;
       }
 
       protected void moveFocus(int x, int y, int status, int time) {
         int oldSelection = getSelectedIndex();
         super.moveFocus(x, y, status, time);
         int newSelection = getSelectedIndex();
 
         if (oldSelection != -1) {
           invalidate(oldSelection);
         }
 
         if (newSelection != -1) {
           invalidate(newSelection);
         } else {
           setSelectedIndex(oldSelection);
           invalidate(oldSelection);
         }
       }
 
       protected void onDisplay() {
         super.onDisplay();
       }
 
       protected void onUndisplay() {
         super.onUndisplay();
       }
     };
     this.listField.setEmptyString("No data!", 6);
 
     this.listData = accountList;
     this.listField.setRowHeight(70);
 
     this.listField.setCallback(this.listFieldCallBack);
 
     int elementLength = accountList.length;
 
     for (int count = 0; count < elementLength; count++)
       this.listField.insert(count);
   }
 
   public ListField getList()
   {
     return this.listField;
   }
 
   public Object getAccountEntrySelected() {
     int index = this.listField.getSelectedIndex();
 
     if ((index > -1) && ((this.listData[index] instanceof AccountEntry))) {
       return this.listData[index];
     }
     return null;
   }
 
   public Object[] getAccountEntriesResponses() {
     return this.listData;
   }
 
   public void setAccountStatus(AccountStatus aStatus) {
     if (aStatus != null)
     {
       this.accountStatus.put(aStatus.getId(), aStatus);
       getList().invalidate();
     }
   }
 
   private class ListCallBack extends AbstractListFieldCallBack {
     private Bitmap tick = Bitmap.getBitmapResource("tick-30x23-p24.png");
 
     public ListCallBack() {
       Bitmap bg = Bitmap.getBitmapResource("trades-row-480x70.png");
       Bitmap bgSelected = Bitmap.getBitmapResource("trades-row-selected-480x70.png");
 
       setBg(bg);
       setBgSelected(bgSelected);
     }
 
     public void drawListRow(ListField list, Graphics graphics, int index, int y, int w) {
       Logger.log("drawListRow");
       int h = list.getRowHeight();
       int fontHeight = 28;
       Font font = DefaultController.getComponentFont().derive(0, fontHeight);
 
       Object o = get(list, index);
 
       drawBackground(graphics, 0, y, w, h, AccountListField.this.listField.getSelectedIndex() == index);
 
       AccountEntry currentRow = null;
 
       if ((o != null) && ((o instanceof AccountEntry))) {
         Logger.log("o!=null && o instanceof AccountEntry");
         currentRow = (AccountEntry)get(list, index);
       } else {
         Logger.log("no data");
         graphics.drawText("No data", 95, y + 6, 0x30 | 0x6);
         return;
       }
 
       Bitmap icon = null;
 
       int height = list.getRowHeight();
 
       if (currentRow != null) {
         if (currentRow.getActive()) {
           icon = this.tick;
           if (icon != null) {
             drawImage(graphics, 32, y, height, icon);
           }
         }
 
         graphics.setColor(16119285);
         graphics.setFont(font.derive(0, fontHeight));
         graphics.drawText(currentRow.getName(), 95, y + (h - 2 - fontHeight) / 2, 0x30 | 0x6);
 
         int x = 264;
         int minWidth = 180;
         graphics.setFont(this.plain22);
         String balance = "----";
         Object as = AccountListField.this.accountStatus.get(currentRow.getId());
         String currency = null;
         if (currentRow.getId() != null) {
           currency = Model.getModel().getAccountCurrencyById(currentRow.getId());
         }
         if (as != null) {
           double b = ((AccountStatus)AccountListField.this.accountStatus.get(currentRow.getId())).getBalance();
           balance = StringUtils.formatCurrency(b);
           balance = balance + " ";
           balance = balance + (currency != null ? currency : "");
         }
         graphics.drawText(balance, x, y + this.verticalPadding + (h - this.plain22.getHeight()) / 2, 0x30 | 0x5, minWidth);
       }
     }
 
     protected void drawBorder(Graphics graphics, int x, int y, int width, int height) {
       graphics.setColor(8421504);
       graphics.drawLine(x - 1, y, x + width - 1, y);
       graphics.drawLine(x - 1, y, x - 1, y + height - 1);
 
       graphics.drawLine(x + width, y - 1, x + width, y + height - 1);
       graphics.drawLine(x - 1, y + height - 1, x + width - 1, y + height - 1);
     }
 
     public Object get(ListField list, int index) {
       return AccountListField.this.listData[index];
     }
   }
}

